<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入职位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recruitList">
      <el-table-column label="职位名称" prop="name"/>
      <el-table-column label="薪资待遇" prop="compensation"/>
      <el-table-column label="职位要求" prop="demand"/>
      <el-table-column label="工作地址" prop="city">
        <template slot-scope="scope">
          <div>{{ scope.row.city }}</div>
          <div>{{ scope.row.address }}</div>
        </template>
      </el-table-column>
      <el-table-column label="公司信息" prop="company">
        <template slot-scope="scope">
          <div>{{ scope.row.company }}</div>
          <div>{{ scope.row.linkman }}·{{ scope.row.job }} - {{ scope.row.phone }}</div>
        </template>
      </el-table-column>
      <el-table-column label="岗位类型" prop="type">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.type === 1 ? '全职' : '兼职' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" prop="createAt" width="180"/>
      <el-table-column label="操作" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招聘对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="职位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入职位名称"/>
        </el-form-item>
        <el-form-item label="职位类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择职位类型" style="width: 100%">
            <el-option label="全职" :value="1"></el-option>
            <el-option label="兼职" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作城市" prop="city">
          <el-cascader
            style="width: 100%;"
            :options="options"
            v-model="selectedOptions"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="工作地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入工作地址"/>
        </el-form-item>
        <el-form-item label="薪资待遇" prop="compensation">
          <el-input v-model="form.compensation" placeholder="请输入薪资待遇"/>
        </el-form-item>
        <el-form-item label="职位要求" prop="demand">
          <el-input v-model="form.demand" placeholder="请输入职位要求"/>
        </el-form-item>
        <el-form-item label="公司名称" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="联系人" prop="linkman">
          <el-input v-model="form.linkman" placeholder="请输入联系人"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="职务" prop="job">
          <el-input v-model="form.phone" placeholder="请输入职务"/>
        </el-form-item>
        <el-form-item label="岗位详情" prop="introduce">
          <Editor v-model="form.introduce" :height="300"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecruit, getRecruit, delRecruit, addRecruit, updateRecruit } from '@/api/app/recruit'
import { provinceAndCityData, codeToText } from 'element-china-area-data'

export default {
  name: 'Recruit',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招聘表格数据
      recruitList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null
      },
      options: provinceAndCityData,
      selectedOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '职位名称不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '岗位类型不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '省份-城市不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '工作地址不能为空', trigger: 'blur' }
        ],
        compensation: [
          { required: true, message: '薪资待遇不能为空', trigger: 'blur' }
        ],
        demand: [
          { required: true, message: '职位要求不能为空', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ],
        linkman: [
          { required: true, message: '联系人不能为空', trigger: 'blur' }
        ],
        job: [
          { required: true, message: '职务不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询招聘列表 */
    getList() {
      this.loading = true
      listRecruit(this.queryParams).then(response => {
        this.recruitList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        city: null,
        address: null,
        compensation: null,
        demand: null,
        company: null,
        linkman: null,
        phone: null,
        job: null,
        type: null,
        introduce: null
      }
      this.resetForm('form')
    },
    //也可以这样写
    handleChange() {
      // 使用 map 方法将 selectedOptions 中的每个 code 转换为对应的文本
      const locationTexts = this.selectedOptions.map(code => codeToText[code])
      this.form.city = locationTexts.join('-')
      console.log(this.form.city)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加招聘'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRecruit(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改招聘'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecruit(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRecruit(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除招聘编号为"' + ids + '"的数据项？').then(function() {
        return delRecruit(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>
